// createRouter用于创建路由的函数
// createWebHashHistory 用于创建哈希路由配置
import { createRouter, createWebHashHistory } from 'vue-router'
import Layout from '@/views/layout.vue'
import Home from '@/views/home/index.vue'
import CategoryIndex from '@/views/category/index.vue'
import CategorySub from '@/views/category/sub.vue'
import GoodsDetail from '@/views/goods/index.vue'
import Login from '@/views/login/index.vue'
import LoginCallBack from '@/views/login/callback.vue'
import Cart from '@/views/cart/index.vue'
import store from '@/store'
import Checkout from '@/views/member/pay/checkout.vue'
import PayIndex from '@/views/member/pay/index.vue'
import payCallback from '@/views/member/pay/callback.vue'
import MemberLayout from '@/views/member/Layout.vue'
import MemberHome from '@/views/member/home/index.vue'
import MemberOrderDetail from '@/views/member/order/detail.vue'
import MemberOrder from '@/views/member/order/index.vue'
import { h } from 'vue'

// 配置路由的映射关系，路劲和组件的映射
const routes = [
  {
    path: '/',
    component: Layout,
    children: [
      { path: '', component: Home },
      { path: 'category/:id', component: CategoryIndex }, // 一级分类页
      { path: 'category/sub/:id', component: CategorySub }, // 二级分类页
      { path: 'product/:id', component: GoodsDetail },
      { path: 'cart', component: Cart }, // 二级分类页
      { path: 'member/checkout', component: Checkout }, // 支付页面
      { path: '/member/pay', component: PayIndex },
      { path: '/member/pay/callback', component: payCallback },
      {
        path: 'member',
        component: MemberLayout,
        children: [
          { path: '', component: MemberHome },
          // { path: 'order', component: MemberOrder },
          // { path: 'order/:id', component: MemberOrderDetail }
          {
            path: 'order',
            component: { render: () => h(<RouterView />) },
            children: [
              { path: '', component: MemberOrder },
              { path: ':id', component: MemberOrderDetail }
            ]
          }
          // {
          //   path: '/member/order',
          //   // vue3.0 需要有嵌套关系才能模糊匹配
          //   component: { render: () => h(<RouterView />) },
          //   children: [
          //     { path: '', component: MemberOrder },
          //     { path: ':id', component: MemberOrderDetail }
          //   ]
          // }

        ]
      }
    ]
  },
  { path: '/login', component: Login },
  { path: '/login/loginCallBack', component: LoginCallBack }
]

// 创建路由实例
const router = createRouter({
  history: createWebHashHistory(),
  routes,
  // 始终滚动到顶部
  scrollBehavior (to, from, savedPosition) {
    if (to.path.startsWith('/category')) {
      return { top: 0 }
    } else {
      return savedPosition
    }
  }
})
// 前置导航守卫
router.beforeEach((to, from, next) => {
  // 用户信息
  const { token } = store.state.user.profile
  // 跳转去member开头的地址却没有登录
  if (to.path.startsWith('/member') && !token) {
    next({ path: '/login', query: { redirectUrl: to.fullPath } })
  }
  next()
})

export default router
